package io.github.novaframe.config;

import io.github.novaframe.common.util.SpringContextHolder;
import io.github.novaframe.quartz.QuartzJobController;
import io.github.novaframe.quartz.QuartzJobService;
import io.github.novaframe.web.mapper.JsonMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Created by Locki on 9/11/2017.
 */
@Configuration
public class QuartzSchedulerConfiguration {
    //private final static String CRON_EXPRESSION = "0/3 * * * * ?";

    // 创建JobDetailFactoryBean。顾名思义，可以用于生成JobDetail
    //@Bean
    //public JobDetailFactoryBean createJobDetail(){
    //    JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();jobDetailFactoryBean.setName("myJobName");
    //    jobDetailFactoryBean.setGroup("myJobGroup");
    //    jobDetailFactoryBean.setJobClass(TestJob.class);
    //
    //    return jobDetailFactoryBean;
    //}


    // 创建Trigger。作用：配置定时时间
    //@Bean
    //protected CronTriggerFactoryBean createTrigger(JobDetail jobDetail) {
    //    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    //    cronTriggerFactoryBean.setName("myTriggerName");
    //    cronTriggerFactoryBean.setGroup("myTriggerGroup");
    //    cronTriggerFactoryBean.setCronExpression(CRON_EXPRESSION);
    //    cronTriggerFactoryBean.setJobDetail(jobDetail);
    //    cronTriggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY);
    //
    //    return cronTriggerFactoryBean;
    //}

    /**
     * 创建SchedulerFactoryBean。作用：启动定时任务
     * SchedulerName : schedulerCluster
     *
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        schedulerFactoryBean.setSchedulerName("testSchedulerCluster");

        // 集群任务
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));

        // 本地任务
        //schedulerFactoryBean.setTriggers(triggers.toArray(new Trigger[0]));

        return schedulerFactoryBean;
    }

    @Bean
    public QuartzJobService quartzJobService(SchedulerFactoryBean schedulerFactoryBean, JsonMapper jsonMapper) {
        return new QuartzJobService(schedulerFactoryBean, jsonMapper);
    }

    @Bean
    public QuartzJobController quartzJobController(QuartzJobService quartzJobService) {
        return new QuartzJobController(quartzJobService);
    }

    @Bean
    public SpringContextHolder springContextHolder(ApplicationContext context) {
        SpringContextHolder holder = new SpringContextHolder();
        holder.setApplicationContext(context);
        return holder;
    }
}
